package abq2.jic.jic.plugin.operate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class SchoolFeedback implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String , String> result = new HashMap<>();
        if ("GET_School_Feedback".equalsIgnoreCase(action)) {
            //获取DynamicObject列表，即获取学生的单据头的信息
            DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_school_feedback", //反馈单据
                    "abq2_feedback_date," +         //反馈日期
                            "number," + //单据编号
                            "abq2_context",           //反馈内容
                    (new QFilter("number", QCP.is_notnull, null)).toArray());

            //创建一个JsonArray
            JSONArray jsonArray = new JSONArray();
            int num = 0;
            for (DynamicObject dynamicObject : dys) {
                if (num > 10){
                    break;
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("feedback_time", new SimpleDateFormat("yyyy-MM-dd").format(dynamicObject.getDate("abq2_feedback_date")));
                jsonObject.put("feedback_information", dynamicObject.getString("abq2_context"));
                jsonArray.add(jsonObject);
                num++;
            }

            System.out.println(jsonArray.toJSONString());
            //加入resultDynamicObject参数，将JsonArray加入到这个参数当中，然后返回
            result.put("resultDynamicObject", jsonArray.toJSONString());
        }
        return result;
    }
}
